package com.work.shart.web.config;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.Lists;
import com.work.shart.web.entity.classifcation.ClassificationEntity;
import com.work.shart.web.entity.goods.GoodsEntity;
import com.work.shart.web.res.goods.GoodsRes;
import com.work.shart.web.service.goods.GoodsEsService;
import com.work.shart.web.service.classifcation.IClassificationService;
import com.work.shart.web.service.goods.IGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class LucenceCommandRunner implements CommandLineRunner {
    @Autowired
    private IClassificationService classificationService;

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private GoodsEsService goodsEsService;


    @Override
    public void run(String... args) {
        log.info("-------初始化文章索引文件-------");
        List<GoodsEntity> goodsEntities = goodsService.list();
        List<GoodsRes> goodsRes = Lists.newArrayList();
        goodsEntities.forEach(goodsEntity -> {
            ClassificationEntity classificationEntity = classificationService.getById(goodsEntity.getClassifationId());
            GoodsRes res = BeanUtil.copyProperties(goodsEntity, GoodsRes.class, "classification");
            if (classificationEntity != null) {
                res.setClassification(classificationEntity);
            }
            goodsRes.add(res);
        });
        goodsEsService.save(goodsRes);
        log.info("-------结束{}-------", goodsRes);
    }
}
